<template>
  <div class="login-container">
    <h1>{{ title }}</h1>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <h3 class="title">用户入口</h3>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input v-model="loginForm.username" name="username" type="text" auto-complete="on" placeholder="username" />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :type="pwdType"
          v-model="loginForm.password"
          name="password"
          auto-complete="on"
          placeholder="password" />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="pwdType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <el-form-item prop="verifycode">
        <el-row type="flex" class="code" justify="space-between">
          <el-col :span="12">
            <el-input v-model="loginForm.verifycode" style="padding-left:20px" placeholder="请输入验证码" class="identifyinput" @keyup.enter.native="handleLogin('loginForm')"/>
          </el-col>
          <el-col :span="12">
            <div class="identifybox">
              <div @click="refreshCode">
                <s-identify :identify-code="identifyCode"/>
              </div>
              <!-- <el-button type="text" class="textbtn" @click="refreshCode">看不清，换一张</el-button> -->
            </div>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" type="primary" style="width:100%;" @click="handleLogin('loginForm')">
          登录
        </el-button>
      </el-form-item>
      <!-- <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span> password: admin</span>
      </div> -->
    </el-form>
  </div>
</template>

<script>
import { login } from '@/api/login'
// import { login } from '@/api/drg-target-report/report-state-query'
// import { isvalidUsername } from '@/utils/validate'
import SIdentify from '@/components/identify/index.vue'

export default {
  name: 'Login',
  components: {
    SIdentify
  },
  data() {
    // const validateUsername = (rule, value, callback) => {
    // if (!isvalidUsername(value)) {
    //   callback(new Error('请输入正确的用户名'))
    // } else {
    //   callback()
    // }
    // }
    const validatePass = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error('密码不能小于5位'))
      } else {
        callback()
      }
    }
    // 验证码自定义验证规则
    const validateVerifycode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'))
      } else if (value !== this.identifyCode) {
        console.log('validateVerifycode:', value)
        callback(new Error('验证码不正确!'))
      } else {
        callback()
      }
    }
    return {
      title: '国家医疗保障信息平台支付方式管理子系统DRG管理模块',
      loginForm: {
        username: '',
        password: '',
        verifycode: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur' }],
        password: [{ required: true, trigger: 'blur', validator: validatePass }],
        verifycode: [{ required: true, trigger: 'blur', validator: validateVerifycode }]
      },
      loading: false,
      pwdType: 'password',
      redirect: undefined,
      identifyCodes: '1234567890',
      identifyCode: ''
    }
  },

  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
    this.identifyCode = ''
    this.makeCode(this.identifyCodes, 4)
  },
  methods: {
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          let params = {
            userCode: this.loginForm.username,
            userPassword: this.loginForm.password
          }
          this.loading = true
          login(params).then(res => {
            if (res.data.code === 0) {
              // this.$store.dispatch('Login', this.loginForm).then(() => {
              window.sessionStorage.setItem('user', JSON.stringify(res.data.data))
              this.loading = false
              this.$router.push({
                path: this.redirect || '/'
                // query: {
                //   id: res.data.data.userType
                // }
              })
              // }).catch(() => {
              //   this.loading = false
              // })
            } else {
              this.$alert(res.data.message, {
                type: 'error',
                confirmButtonText: '确定'
              })
              this.loading = false
            }
          }).catch(err => {
            this.loading = false
            console.log(err)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 生成随机数
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min)
    },
    // 切换验证码
    refreshCode() {
      this.identifyCode = ''
      this.makeCode(this.identifyCodes, 4)
    },
    // 生成四位随机验证码
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode += this.identifyCodes[
          this.randomNum(0, this.identifyCodes.length)
        ]
      }
      // console.log(this.identifyCode)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
h1 {
  width: 1200px;
  margin: 2% auto;
  color: $--color-primary;
}
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  // background-image: url(../../assets/banner.jpg);
  background-image: url(../../assets/background.jpg);
  background-repeat: no-repeat;
  background-size: 100% 60%;
  background-position: center center;
  & /deep/ .el-input {
    display: inline-block;
    height: 40px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 9px 0;
      color: $text-color-text;
      height: 40px;
      // &:-webkit-autofill {
      //   -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
      //   -webkit-text-fill-color: #fff !important;
      // }
    }
  }

  .el-button.el-button--small {
    height: 40px;
  }

  .el-form-item {
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }

  .login-form {
    position: absolute;
    top: 50%;
    right: 20%;
    width: 370px;
    max-width: 100%;
    padding: 35px 35px 15px 35px;
    transform: translateY(-50%);
    background-color: #fff;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    color: $text-color-minor;
    vertical-align: middle;
    width: 40px;
    text-align: center;
    display: inline-block;
  }
  .title {
    font-size: 26px;
    font-weight: 400;
    color: $text-color-text;
    margin: 0px auto 40px auto;
    text-align: center;
    font-weight: bold;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    cursor: pointer;
    user-select: none;
    color: $text-color-minor;
  }
}
</style>
